import CryptoJS from 'crypto-js'

//AES-256-CBC
export const encryptCBC = (plaintext, key) => {
    const skey = CryptoJS.enc.Utf8.parse(key);
    const siv = CryptoJS.lib.WordArray.random(16);
    const encrypted = CryptoJS.AES.encrypt(plaintext, skey, {
        iv: siv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });
    return siv.toString(CryptoJS.enc.Hex) + encrypted.toString(); // Hex IV + Base64 Ciphertext
};

export const decryptCBC = (combinedData, key) => {
    const skey = CryptoJS.enc.Utf8.parse(key);
    const ivHex = combinedData.substring(0, 32); // Hex IV 占前32字符
    const siv = CryptoJS.enc.Hex.parse(ivHex);
    const ciphertextBase64 = combinedData.substring(32);
    const decrypted = CryptoJS.AES.decrypt(ciphertextBase64, skey, {
        iv: siv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
};

export const encryptMD5 = (content) => {
    return CryptoJS.MD5(content).toString();
};




